#include "settings/settings.h"
#include "settings/settings_parser.h"
#include "xml/xml_attributes.h"

namespace Tvimeg
{
	const std::string VIDEO_PATH_ELEMENT("video_path");
	const std::string TYPE_ATTRIBUTE("type");
	const std::string STRING_ATTRIBUTE("string");
	const std::string PATH_ATTRIBUTE("path");
	const std::string MOVIE_TYPE("movies");
	const std::string TV_TYPE("tv_series");
	const std::string FILE_TYPE_ELEMENT("supported_file_type");
	const std::string MULTI_PART_ELEMENT("multi_part_string");
	const std::string DATABASE_ELEMENT("database");

	SettingsParser::SettingsParser(Settings& settings_in)
		:	settings(settings_in)
	{
	}

	bool SettingsParser::Parse(const File& file)
	{
		settingsDirectory = file.GetParent();
		return XMLParser::Parse(file);
	}

	void SettingsParser::StartElement(const std::string& elementName, const XMLAttributes& attributes)
	{
		if (elementName == VIDEO_PATH_ELEMENT)
		{
			const std::string& type = attributes.GetAttribute(TYPE_ATTRIBUTE);
			const Directory directory = settingsDirectory.GetRelativeDirectory(attributes.GetAttribute(PATH_ATTRIBUTE));
			if (type == MOVIE_TYPE)
			{
				settings.AddMovieDirectory(directory);
			}
			else if (type == TV_TYPE)
			{
				settings.AddTVSeriesDirectory(directory);
			}
		}
		else if (elementName == FILE_TYPE_ELEMENT)
		{
			settings.AddFileType(attributes.GetAttribute(TYPE_ATTRIBUTE));
		}
		else if (elementName == MULTI_PART_ELEMENT)
		{
			settings.AddMultiPart(attributes.GetAttribute(STRING_ATTRIBUTE));
		}
		else if (elementName == DATABASE_ELEMENT)
		{
			settings.SetDatabaseFile(settingsDirectory.GetRelativeFile(attributes.GetAttribute(PATH_ATTRIBUTE)));
		}
	}

	void SettingsParser::EndElement(const std::string&)
	{
	}

	void SettingsParser::CharacterData(const std::string&)
	{
	}
}
